{*SMARTY*}
{add_layout file='common_layout'}
{block name=meta_title}ADD ADODB Admin{/block}
{block name=styles}
   <link href="//add-presentability.googlecode.com/svn/project/tags/1.2/all.css" rel="stylesheet" type="text/css" />
   <style>
      body.mode_query .page {
         width:auto;
         margin-left:20px;
         margin-right:auto;
      }
      .main .table a {
         font-weight:bold;
      }
      .main form.query {
         width:91%;
      }
      .main form.query textarea {
         height:300px;
      }
      .main .loading {
         margin:50px auto;
         padding-top:42px;
         background:url(assets/images/loading.gif) center 5px no-repeat;
         text-align:center;
      }
   </style>
{/block}
{block name='header.home_link.text'}ADD ADOdb Browser{/block}
{block name=nav}
{if adodb_user::current_logged_in()}
<ul>
   <li><a href="{add::config()->base_url}">Home</a></li>
   <li style="text-align:right"><a href="{add::config()->base_url}login?mode=logout">Logout</a></li>
</ul>
{/if}
{/block}
{block name=post_body_scripts append}
   <script type="text/javascript">
      $ajax_forms = $('form[data-ajax-target]');

      var $query_form = $ajax_forms.filter('.query');
      var $textarea = $query_form.find('textarea');

      $textarea.keyup_change(
            function() {
               if ( ($query_form.serialize().length + location.pathname.length) > 512 ) {
                  $query_form.attr('method','post');
               }
               else {
                  $query_form.attr('method','get');
               }
            }
         );

      $ajax_forms.submit(
            function () {
               return ajax_submit_form(this);
            }
         );

      function ajax_submit_form(form_selector) {
         var $this = $(form_selector);
         var ajax_settings = {};
         var $title = $('title:eq(0)');

         if ($this.hasClass('ajax-cancelled')) {
            $this.removeClass('ajax-cancelled');
            return false;
         }

         if ($this.attr('method'))
            ajax_settings.type = $this.attr('method');


         ajax_settings.data = $this.serialize();

         if (ajax_settings.type.toLowerCase()  == "get") {
            if (window.history.pushState)
               window.history.pushState({},null, '?' + ajax_settings.data);
         }
         ajax_settings.data += '&ajax=1';

         if ($this.attr('data-ajax-target')) {
            var $target = $($this.attr('data-ajax-target'));
            $title.html('(Loading...)'+$title.html());
            $target.html('<div class="loading">Loading...</div>');
         }


         ajax_settings.complete = function (response) {
               process_json(response.responseText,$target)
               $title.html($title.html().replace(/^\(Loading\.\.\.\)/,''));
            };

         $.ajax(
               $this.attr('action') ? $this.attr('action') : '',
               ajax_settings
            );

         return false;
      }

      $ajax_links = $('a[data-ajax-target]');

      $ajax_links.click(
            function () {
               var $this = $(this);
               var $target;
               var $title = $('title:eq(0)');

               if ($this.attr('data-ajax-target')) {
                  var $target = $($this.attr('data-ajax-target'));
                  $title.html('(Loading...)'+$title.html());
                  $target.html('<div class="loading">Loading...</div>');
               }

               $.get($this.attr('href'),{ ajax:1 },function(data) { process_json(data,$target); $title.html($title.html().replace(/^\(Loading\.\.\.\)/,'')); });
               return false;
            }
         );


      function process_json(json_string,target) {
         var json = $.parseJSON(json_string);
         var $target = $(target);
         switch (json.type) {
            case 'eval':
               $target.html("<div>Executing...</div>");
               eval(json.script);
            break;
            case 'message':
            case 'error_message':
            break;
            default:
               alert("An unknown error occured");
            break;
         }
         if (json.message) {
            if ($target) {
               $target.html('<div>'+json.message+'</div>');
            }
            else {
               alert(json.message);
            }
         }
         else if (json.error_message) {
            if ($target) {
               $target.html('<div class="error_message">'+json.error_message+'</div>');
            }
            else {
               alert(json.error_message);
            }
         }
      }
   </script>
{/block}